#! /usr/bin/env python
import waflib.Task
from waflib.TaskGen import extension
from waf_dynamo import copy_file_task

def create_simple_protoc_task(name, ext, compiled_ext, type, before, shell = True, color = 'PINK', proto_file = '../../particle/proto/particle/particle_ddf.proto'):
    def create(self, node):
        task = self.create_task(name)
        task.set_inputs(node)
        out = node.change_ext(compiled_ext)
        task.set_outputs(out)

    cmd = 'protoc --encode=%s -I ../../particle/proto/particle -I ${DYNAMO_HOME}/share/proto -I ${DYNAMO_HOME}/ext/include %s < ${SRC} > ${TGT}' % (type, proto_file)
    waflib.Task.task_factory(name, cmd, before=before, shell=shell, color=color)
    extension(ext)(create)

create_simple_protoc_task('particle_fx', '.particlefx', '.particlefxc', 'dmParticleDDF.ParticleFX', before='c cxx')

def build(bld):
    uselib = ['TESTMAIN', 'APP', 'SCRIPT', 'GRAPHICS_NULL', 'EXTENSION', 'RESOURCE', 'PARTICLE', 'RIG', 'HID_NULL', 'DDF', 'DLIB', 'PROFILE_NULL', 'PLATFORM_NULL', 'SOCKET', 'LUA', 'GRAPHICS_PROTO', 'GAMEOBJECT']

    test_gui = bld.program(features = 'cxx cprogram embed test',
                           includes = '. ..',
                           use = uselib + ['gui'],
                           web_libs = ['library_sys.js', 'library_script.js'],
                           target = 'test_gui',
                           source = 'test_gui.cpp test_gui_ddf.proto once.particlefx once_three_emitters.particlefx',
                           embed_source = 'bug352.lua')

    test_gui_script = bld.program(features = 'cxx cprogram embed test',
                    includes = '. ..',
                    use = uselib + ['gui'],
                    web_libs = ['library_sys.js', 'library_script.js'],
                    target = 'test_gui_script',
                    source = 'test_gui_script.cpp')

    test_gui_clipping = bld.program(features = 'cxx cprogram embed test',
                    includes = '. ..',
                    use = uselib + ['gui'],
                    web_libs = ['library_sys.js', 'library_script.js'],
                    target = 'test_gui_clipping',
                    source = 'test_gui_clipping.cpp')

    bld.add_group()

    # Note that these null tests won't actually work since the tests aren't written that way.
    # The test is instead that the executables link properly (so that we don't miss any unresolved symbols)
    # We do this by removing the 'test' feature which excludes it from the test run
    for test in (test_gui, test_gui_script, test_gui_clipping):
        bld.program(features = 'cxx cprogram embed test skip_test',
                        source = test.source,
                        embed_source = 'bug352.lua',
                        use = uselib + ['gui_null'],
                        web_libs = ['library_sys.js', 'library_script.js'],
                        includes = '. ..',
                        target = test.target + '_null')
